package com.example.mvp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.baselibrary.R;
import com.example.inter.ListViewRefreshInterface;
import com.example.inter.RefreshLoadMoreInferface;


/**
 * @author chaohaipeng  on 2018/4/2.
 */

public class RefreshListView extends ListView implements  AbsListView.OnScrollListener,ListViewRefreshInterface {
    private ProgressBar pull_to_refresh_progress;
    private TextView tv_refresh_title;
    /**
     * 当前是刷新 false-否 true-是
     */
    private boolean isRefresh = false;
    /**
     * 当前页数
     */
    private int currentPage = 1;
    /**
     * 服务器返回的总页数
     */
    private int totalPage;
    /**
     * 总的数据条数
     */
    private int count;
    private RefreshLoadMoreInferface refreshLoadMoreInferface;

    private ListViewRefreshInterface listViewRefreshInterface;

    public RefreshListView(Context context) {
        super(context);
    }

    public RefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RefreshListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 添加上拉加载进度条布局
     */
    public void initFooterView(Context context, RefreshLoadMoreInferface refreshLoadMoreInferface){
        setOnScrollListener(this);
        this.refreshLoadMoreInferface=refreshLoadMoreInferface;
        setListViewRefreshInterface(this);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_listview_end, null);
        pull_to_refresh_progress =  view.findViewById(R.id.pull_to_refresh_progress);
        tv_refresh_title =  view.findViewById(R.id.tv_refresh_title);
        this.addFooterView(view, null, false);
    }

    public ListViewRefreshInterface getListViewRefreshInterface() {
        return listViewRefreshInterface;
    }

    public void setListViewRefreshInterface(ListViewRefreshInterface listViewRefreshInterface) {
        this.listViewRefreshInterface = listViewRefreshInterface;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (tv_refresh_title==null||pull_to_refresh_progress==null||refreshLoadMoreInferface==null){
            return;
        }
        if (!isRefresh && view.getLastVisiblePosition() != 0 && view.getLastVisiblePosition() == (view.getCount() - 1)) {
            isRefresh=true;
            if (currentPage >= totalPage) {
                tv_refresh_title.setVisibility(View.GONE);
                pull_to_refresh_progress.setVisibility(View.GONE);
            } else {
                //请求服务器数据
                tv_refresh_title.setText("加载中...");
                tv_refresh_title.setVisibility(View.VISIBLE);
                pull_to_refresh_progress.setVisibility(View.VISIBLE);
                refreshLoadMoreInferface.loadMoreData();

            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (tv_refresh_title==null||pull_to_refresh_progress==null){
            return;
        }
        if (totalItemCount > 1) {
            if (totalItemCount == count + 1) {
                tv_refresh_title.setVisibility(View.GONE);
                pull_to_refresh_progress.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void loadMore(int current, int totalPage, int count) {
        if (tv_refresh_title==null||pull_to_refresh_progress==null){
            return;
        }
        this.currentPage=current;
        this.totalPage=totalPage;
        this.count=count;
        isRefresh=false;
        tv_refresh_title.setVisibility(View.GONE);
        pull_to_refresh_progress.setVisibility(View.GONE);
    }
}
